package pl.put.to.classification.utils;

import java.util.List;

import Jama.Matrix;

public class MatrixUtils {
	public static Matrix convertToMatrix(double[][] array) {
		Matrix result = new Matrix(array);

		return result;
	}

	public static Matrix convertToVector(double[] array) {
		Matrix result = null;

		double[][] tempArray = new double[1][array.length];
		tempArray[0] = array;

		result = new Matrix(tempArray);

		return result;
	}

	public static Matrix convertToVector(List<Double> list) {
		Matrix result = null;

		double[] array = new double[list.size()];

		for (int i = 0; i < list.size(); ++i) {
			array[i] = list.get(i);
		}

		result = convertToVector(array);

		return result;
	}

	public static double[][] convertToArray(Matrix matrix) {
		double[][] result = matrix.getArray();

		return result;
	}

	public static double[] convertToVectorArray(Matrix matrix) {
		double[] result = null;

		double[][] tempArray = matrix.getArray();

		if (tempArray.length != 1) {
			throw new IllegalArgumentException("Given matrix is not a vector (it has more than 1 rows)");
		}

		result = tempArray[0];

		return result;
	}

	public static double convertToScalar(Matrix matrix) {
		double result = 0;

		if (matrix.getColumnDimension() != 1 || matrix.getRowDimension() != 1) {
			throw new IllegalArgumentException("Given matrix is not a scalar (it has more than 1 elements)");
		}

		result = matrix.get(0, 0);

		return result;
	}
}
